package cn.zhangxin.pethome.system.menu.service.impl;

import cn.zhangxin.pethome.system.menu.domain.Menu;
import cn.zhangxin.pethome.system.menu.mapper.MenuMapper;
import cn.zhangxin.pethome.system.menu.service.IMenuService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import cn.zhangxin.pethome.utils.BusinessException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-02-01
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    public static String MENU_LIST="menuList";
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Menu> getParentName() {
        return menuMapper.getParentName();
    }

    //获得菜单树
    @Override
    public List<Menu> getMenuTree() {
        List<Menu> menus = menuMapper.findAll();
        //过滤ParentId为null的数据 就是父菜单
        List<Menu> parentMenu = menus.stream()
                .filter(menu -> menu.getParentId() == null)
                .collect(Collectors.toList());
        for (Menu menu : parentMenu){
            //通过父id查询所有只菜单，并封装进对象中
            menu.setChildren(menuMapper.findAllByParentId(menu.getId()));
        }
        return parentMenu;
    }

    @Override
    public List<Menu> findAllByEmployeeId(Long employeeId) {
        return menuMapper.findAllByEmployeeId(employeeId);
    }

    //菜单树
    @Override
    public List<Long> findMenuByRoleIds(List<Long> roles) {
        return menuMapper.findMenuByRoleIds(roles);
    }

    @Override
    public void add(Menu menu) {
        Integer indexMax = menuMapper.getIndexMax(menu.getParentId());
        menu.setIndex(indexMax+1);
        super.add(menu);
        redisTemplate.opsForValue().set(MENU_LIST,super.findAll(),1, TimeUnit.DAYS);
    }

    @Override
    public void update(Menu menu) {
        //当菜单还有子菜单时，说明为父菜单，则不能修改
        List<Menu> allByParentId = menuMapper.findAllByParentId(menu.getId());
        if (allByParentId.size()!=0){
            throw new BusinessException("当前菜单为父菜单不能修改");
        }
        //获得父菜单id 查询出最大的index
        Integer indexMax = menuMapper.getIndexMax(menu.getParentId());
        //如果查询出来的Parentid和修改前的Parentid相同，说明并index并没有发送改变，还是保存自己的index
        Menu menus = menuMapper.findOne(menu.getId());
        if (menus.getParentId() != menu.getParentId()) {
            //设置index
            menu.setIndex(indexMax + 1);
        }
        menuMapper.update(menu);
        redisTemplate.opsForValue().set(MENU_LIST,super.findAll(),1, TimeUnit.DAYS);
    }

    @Override
    public void delete(Long id) {
        List<Menu> allByParentId = menuMapper.findAllByParentId(id);
        if (allByParentId.size()==0){//当没有数据时，执行删除
            menuMapper.delete(id);
        }else {
            throw new BusinessException("该菜单下还存在字菜单请勿重复删除！");
        }
    }

    @Override
    public List<Menu> findAll() {
        if (redisTemplate.opsForValue().get(MENU_LIST)==null){
            List<Menu> all = super.findAll();
            redisTemplate.opsForValue().set(MENU_LIST,all,1, TimeUnit.DAYS);
            return all;
        }else {
            return (List<Menu>)redisTemplate.opsForValue().get(MENU_LIST);
        }
    }
}
